import formatTime from '../../utils/formatTime.js'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        blogId: '',
        blog: {},
        commentList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            blogId: options.blogId
        })
        this.getBlogDetail()
    },

    // 获取博客详情
    getBlogDetail() {
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        
        wx.cloud.callFunction({
            name: 'blog',
            data: {
                blogId: this.data.blogId,
                $url: 'detail'
            }
        }).then(res => {
            console.log('res', res)
            const blog = res.result.detail[0]
            const commentList = res.result.commentList.data

            for(let i = 0, len = commentList.length; i < len; i++) {
                commentList[i].createTime = formatTime(new Date(commentList[i].createTime))
            }

            this.setData({
                blog,
                commentList
            })

            wx.hideLoading()
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        const blog = this.data.blog
        return {
            title: blog.content,
            path: `/pages/blog-detail/blog-detail?blogId=${blog._id}`
        }
    }
})